package com.autotest.nore.service;

import com.alibaba.fastjson2.JSONObject;
import com.autotest.nore.domain.Report;
import com.autotest.nore.domain.ReportCollectionCaseApi;
import com.autotest.nore.dto.*;
import com.autotest.nore.mapper.*;
import com.autotest.nore.request.QueryRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private ReportCollectionMapper reportCollectionMapper;

    @Autowired
    private ReportCollectionCaseMapper reportCollectionCaseMapper;

    @Autowired
    private ReportCollectionCaseApiMapper reportCollectionCaseApiMapper;
    @Autowired
    private PlanCollectionMapper planCollectionMapper;

    public void deleteReport(Report report) {
        reportCollectionCaseApiMapper.deleteByReportId(report.getId());
        reportCollectionCaseMapper.deleteByReportId(report.getId());
        reportCollectionMapper.deleteByReportId(report.getId());
        reportMapper.deleteReport(report.getId());
    }

    public List<ReportDTO> getReportList(QueryRequest request){
        if(request.getCondition() != null && !request.getCondition().equals("")){
            request.setCondition(("%"+request.getCondition()+"%"));
        }
        return reportMapper.getReportList(request);
    }
    public List<ReportDTO> getPlanReportList(QueryRequest request){
        if(request.getCondition() != null && !request.getCondition().equals("")){
            request.setCondition(("%"+request.getCondition()+"%"));
        }
        String planId=request.getPlanId();
        String projectId=request.getProjectId();
        List<PlanCollectionDTO> planCollectionDTOS=planCollectionMapper.getPlanCollectionList(planId);
        for (int i = 0; i < planCollectionDTOS.size(); i++) {
            String collectionId=planCollectionDTOS.get(i).getCollectionId();
            reportMapper.getReportList2(projectId,collectionId);
        }
        return null;
    }

    public JSONObject getLastApiReport(String apiId){
        JSONObject result = new JSONObject();
        String report = reportCollectionCaseApiMapper.getLastApiReport(apiId);
        if(report == null){
            return null;
        }
        if(!report.contains("<br><b>响应体: ") || !report.contains("</b><br><br>")){
            return null;
        }
        String response = report.substring(report.indexOf("<br><b>响应体: ")+12, report.indexOf("</b><br><br>"));
        try{
            result = JSONObject.parseObject(response);
        }catch (Exception e){
            return result;
        }
        return result;
    }


    public ReportDTO getPlanResult(String reportId){
        ReportDTO report = reportMapper.getReportDetail(reportId);
        List<ReportCollectionDTO> reportCollectionList = reportCollectionMapper.getReportCollectionList(reportId);
        for(ReportCollectionDTO reportCollection:reportCollectionList){
            List<ReportCollectionCaseDTO> reportCollectionCaseList = reportCollectionCaseMapper.getReportCollectionCaseList(reportCollection.getId());
            for(ReportCollectionCaseDTO reportCollectionCase: reportCollectionCaseList){
                List<ReportCollectionCaseTransDTO> transList = null;
                if(reportCollectionCase.getCaseType().equals("API")){
                    transList = reportCollectionCaseApiMapper.getReportCaseActionList(reportCollectionCase.getId());
                }
                reportCollectionCase.setTransList(transList);
            }
            reportCollection.setCaseList(reportCollectionCaseList);
            Integer passCount = reportCollectionCaseMapper.countReportCollectionResultTrue(reportCollection.getId());
            Integer failCount = reportCollectionCaseMapper.countReportCollectionResultFalse(reportCollection.getId());
            Integer errorCount = 0;
            reportCollection.setPassCount(passCount);
            reportCollection.setFailCount(failCount);
            reportCollection.setErrorCount(errorCount);
        }
        report.setCollectionList(reportCollectionList);
        return report;
    }

    public ReportCollectionCaseApi getReport(String reportCollectionCaseId){
        ReportCollectionCaseApi res=reportCollectionCaseApiMapper.getReport(reportCollectionCaseId);
        return res;
    }

    public List<ReportCollectionCaseApi> getCaseReport(String reportCollectionId){
        String reportCollectionCaseId=reportCollectionCaseMapper.getCaseId(reportCollectionId);
        List<ReportCollectionCaseApi> reportCollectionCaseApiList=reportCollectionCaseApiMapper.getReportList(reportCollectionCaseId);
        return reportCollectionCaseApiList;
    }


}
